package com.sita.bluetactivity.Base;

import android.graphics.Color;
import android.location.GpsSatellite;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.TextureMapView;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.LatLngBounds;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.PolygonOptions;
import com.amap.api.maps.model.Polyline;
import com.amap.api.maps.model.PolylineOptions;
import com.sita.bluetactivity.R;
import com.sita.bluetactivity.TripFragment.GpsCorrect;
import com.sita.bluetactivity.TripFragment.TripPointBean;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by Jason on 2016/10/4.
 */

public class BaseMapFragment extends Fragment {
    private TextureMapView mapView;
    private List<LatLng> latLngs = new ArrayList<LatLng>();
    private UiSettings uiSettings;
    private static BaseMapFragment fragment = null;
    private AMap aMap;

    public static  BaseMapFragment newInstance(){
        if (fragment==null){
            synchronized (BaseMapFragment.class){
                if (fragment==null){
                    fragment = new BaseMapFragment();
                }
            }
        }
        return fragment;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.base_map,null);
        EventBus.getDefault().register(this);
        mapView = (TextureMapView) view.findViewById(R.id.base_map_view);
        mapView.onCreate(savedInstanceState);
        return view;
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initMap();
    }
    private void initMap(){
        if (aMap==null){
            aMap = mapView.getMap();
        }

        uiSettings = aMap.getUiSettings();
        uiSettings.setZoomControlsEnabled(false);
    }
    @Override
    public void onDestroy() {
        super.onDestroy();

        EventBus.getDefault().unregister(this);
        mapView.onDestroy();
    }


    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    private void drawTrip(List<TripPointBean.PointsBean> beans){
        PolylineOptions polylineOptions = new PolylineOptions().width(15).color(getResources().getColor(R.color.tripline));
        int size = beans.size();
        LatLng fromLatlng = null;
        LatLng toLatlng = null;
        for (int i = 0; i < size; i++) {
            LatLng latLng = GpsCorrect.transform(beans.get(i).getLatitude(), beans.get(i).getLongitude());
            polylineOptions.add(latLng);
            if (i == 0) {
                fromLatlng = latLng;
                MarkerOptions markerOptions = new MarkerOptions()
                        .icon(BitmapDescriptorFactory.fromResource(R.mipmap.location_a))
                        .draggable(false)
                        .position(latLng);
                aMap.addMarker(markerOptions);
            }
            if (i == size - 1) {
                toLatlng = latLng;
                MarkerOptions markerOptions = new MarkerOptions()
                        .icon(BitmapDescriptorFactory.fromResource(R.mipmap.location_b))
                        .draggable(false)
                        .position(latLng);
                aMap.addMarker(markerOptions);
            }
        }

        aMap.addPolyline(polylineOptions);


        LatLngBounds bounds;
        bounds = new LatLngBounds.Builder()
                .include(fromLatlng).build();

        if (bounds != null && toLatlng != null) {
            bounds = bounds.including(toLatlng);
        }
        // 移动地图，所有marker自适应显示。LatLngBounds与地图边缘10像素的填充区域
        aMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100));
    }
    public void onEventMainThread(List<TripPointBean.PointsBean>  event) {
     //   mTraceClient = new LBSTraceClient(this.getApplicationContext());
        drawTrip(event);
    }
}
